package org.texteasy;

import java.util.ArrayList;
import java.util.HashSet;

import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Build;

public abstract class ContactsAccessor {

	private static ContactsAccessor instance;
	
	private static final int ECLAIR = 5;
	
	public synchronized static ContactsAccessor getInstance(){
		
		if(instance == null){
			String className;

            /*
             * Check the version of the SDK we are running on. Choose an
             * implementation class designed for that version of the SDK.
             *
             * Unfortunately we have to use strings to represent the class
             * names. If we used the conventional ContactAccessorSdk5.class.getName()
             * syntax, we would get a ClassNotFoundException at runtime on pre-Eclair SDKs.
             * Using the above syntax would force Dalvik to load the class and try to
             * resolve references to all other classes it uses. Since the pre-Eclair
             * does not have those classes, the loading of ContactAccessorSdk5 would fail.
             */
            int sdkVersion = Integer.parseInt(Build.VERSION.SDK);       // Cupcake style
            if (sdkVersion >= ECLAIR) {
                className = "org.texteasy.ContactsAccessorSdk5";
            } else {
                className = "org.texteasy.ContactsAccessorSdk3_4";
            }

            /*
             * Find the required class by name and instantiate it.
             */
            try {
                Class<? extends ContactsAccessor> clazz =
                        Class.forName(className).asSubclass(ContactsAccessor.class);
                instance = clazz.newInstance();
            } catch (Exception e) {
                throw new IllegalStateException(e);
            }

		}
		
		return instance;
	}
	
	public abstract Cursor queryContacts(ContentResolver contentResolver, String filter, long selectedGroupId, 
			ArrayList<Integer> phoneTypes);
	
	public abstract Cursor queryGroups(ContentResolver contentResolver);
	
	public abstract Cursor queryContactsForPhoneNumbers(ContentResolver contentResolver, HashSet<Long> selectedIds);
	
	public abstract String getPhoneColumnIndex();
	
}
